//
//  ViewController.swift
//  C150803_UIView_Event
//
//  Created by yao_yu on 15/8/3.
//  Copyright © 2015年 yao_yu. All rights reserved.
//

import UIKit

protocol ReportDelegate {
    func printMsg(msg: String)
}

class MyView:UIView {
    
    var reportDelegate: ReportDelegate?
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        reportDelegate?.printMsg("willMoveToSuperview[\(newSuperview)]")
    }
    
    override func didMoveToSuperview() {
        reportDelegate?.printMsg("didMoveToSuperview[\(superview)]")
    }
    
    override func didMoveToWindow() {
        reportDelegate?.printMsg("didMoveToWindow[\(superview)]")
    }
}

extension ViewController: ReportDelegate {
    func printMsg(msg: String) {
        resultLabel.text = "\(msg)\r\n\(resultLabel.text!)"
    }
}

class ViewController: UIViewController {

    var myView = MyView()
    var resultLabel = UILabel()
    var commandPane = UIView()
    var documentController: UIDocumentInteractionController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
    }
    
    func setupViews() {
    
        var button = UIButton(type: UIButtonType.System)
        button.setTitle("添加", forState: UIControlState.Normal)
        button.frame = CGRectMake(0, 0, 80, 40)
        button.addTarget(self, action: "onShowView", forControlEvents: UIControlEvents.TouchUpInside)
        commandPane.addSubview(button)
        
        button = UIButton(type: UIButtonType.System)
        button.setTitle("移除", forState: UIControlState.Normal)
        button.frame = CGRectMake(100, 0, 80, 40)
        button.addTarget(self, action: "onHideView", forControlEvents: UIControlEvents.TouchUpInside)
        commandPane.addSubview(button)
        
        commandPane.frame = CGRectMake(0, 20, 180, 40)
        view.addSubview(commandPane)
        
        myView.frame = CGRectMake(0, 60, 200, 100)
        myView.backgroundColor = UIColor.redColor()
        myView.reportDelegate = self
        
        resultLabel.frame = CGRectMake(0, 160, 400, 300)
        resultLabel.numberOfLines = 100
        resultLabel.text = ""
        view.addSubview(resultLabel)
    }
    
    func onShowView() {
        view.addSubview(myView)
        var filePath = NSBundle.mainBundle().pathForResource("测试结果2", ofType: "pdf")
        self.documentController = UIDocumentInteractionController(URL: NSURL(fileURLWithPath: filePath!))
        self.documentController?.UTI = filePath
        documentController?.presentOpenInMenuFromRect(CGRectZero, inView: view, animated: true)
    }
    
    func onHideView() {
        myView.removeFromSuperview()
    }
    
    override func viewDidLayoutSubviews() {
        commandPane.center.x = view.bounds.width / 2
        myView.center.x = view.bounds.width / 2
        resultLabel.frame = CGRectMake(0, 160, view.bounds.width, view.bounds.height - 160)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

/*
NSString *filePath =
[[NSBundle mainBundle]
pathForResource:@"PDF Document" ofType:@"pdf"];

NSLog(@"file path is: %@", filePath);


self.documentController =
[UIDocumentInteractionController
interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
self.documentController.delegate = self;

self.documentController.UTI = @"com.adobe.pdf";
[self.documentController presentOpenInMenuFromRect:CGRectZero
inView:self.view
animated:YES];
*/

